package com.tledu.xpath;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Xpath1 {
    private static Document doc;
    private static XPath xpath;

    public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
        // 创建Document对象
        // 创建一个初始化Document的工厂
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 不进行校验
        dbf.setValidating(false);
        // 基于工厂创建DocumentBuilder,builder就可以构建出来具体Document
        DocumentBuilder db = dbf.newDocumentBuilder();
        // 这个文件就是我们要解析的xml文件
        doc = db.parse(new FileInputStream(new File("C:\\Users\\cyrus\\Desktop\\经贸20级生产实习\\课堂案例\\day07\\src\\main\\resources\\rss.xml")));

        // 创建XPath对象
        XPathFactory factory = XPathFactory.newInstance();
        xpath = factory.newXPath();
        System.out.println("====================================");
        // 路径查找
//        path();
//        descendantLookup();
//        wildcard();
//        attributeMatch();
        contentFiltering();
    }

    public static void path() throws XPathExpressionException {
        // 路径查找的操作
        Node node = (Node) xpath.evaluate("/rss/channel/title", doc, XPathConstants.NODE);
        // 操作这个节点
        System.out.println("节点名字:"+node.getNodeName());
        System.out.println("获取内部的文本:"+node.getTextContent());
    }

    public static void descendantLookup() throws XPathExpressionException {
        // / 代表的是直接子代， // 代表的是后代
        // 代表的是rss下的所有title标签，不管是子集还是后代都会包含
//       NodeList list = (NodeList) xpath.evaluate("/rss/channel/title", doc, XPathConstants.NODESET);
       NodeList list = (NodeList) xpath.evaluate("/rss/channel//title", doc, XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            // 获取每一项的node
            Node node = list.item(i);
            System.out.println("内容："+node.getTextContent());
        }
    }

    public static void wildcard() throws XPathExpressionException {
//        NodeList list = (NodeList) xpath.evaluate("/rss/channel/*", doc, XPathConstants.NODESET);
        NodeList list = (NodeList) xpath.evaluate("/rss/channel//*", doc, XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            // 获取每一项的node
            Node node = list.item(i);
            System.out.println("标签："+node.getNodeName());
        }
    }

    // 属性选择
    public static void attributeMatch() throws XPathExpressionException {
//        NodeList list = (NodeList) xpath.evaluate("/rss/channel/*", doc, XPathConstants.NODESET);
        // node()任意类型的节点
        NodeList list = (NodeList) xpath
                .evaluate("//node()[@name='stu1']",
                        doc,
                        XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            // 获取每一项的node
            Node node = list.item(i);
            System.out.println("标签："+node.getNodeName());
        }
    }

    // 内容筛选
    public static void contentFiltering() throws XPathExpressionException {
//        NodeList list = (NodeList) xpath.evaluate("/rss/channel/*", doc, XPathConstants.NODESET);
        // node()任意类型的节点
        NodeList list = (NodeList) xpath
                .evaluate("//book[price > 30]/title",
                        doc,
                        XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            // 获取每一项的node
            Node node = list.item(i);
            System.out.println("书："+node.getTextContent());
        }
    }
}
